package com.s3u4.concurrence;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Captain on 27/03/2018.
 */
public class Client {

    public static int count = 0;
    public static Lock lock = new ReentrantLock();

    /**
     * Cost Time : 23
     */
    public static synchronized void increase(){
        count++;
    }

    public static void increase2(){
        lock.lock();
        count++;
        lock.unlock();
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(10);

        long start = System.currentTimeMillis();

        for ( int i = 0 ; i < 10 ; i++ ){
            executor.execute(new Runnable() {
                public void run() {
                    for ( int j = 0 ; j < 10000 ; j++){
                        increase();
                    }
                }
            });
        }

        while ( count < 10 * 10000 ){
            Thread.yield();
        }

        System.out.println(count);

        long end = System.currentTimeMillis();
        System.out.println("Cost Time : " + (end - start));

        executor.shutdown();
    }

}
